<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-10 14:00:06
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-27 23:01:38
 */


namespace common\addons\diandi_shop\api;

use Yii;
use api\controllers\AController;
use common\addons\diandi_shop\services\CartService;
use common\helpers\ResultHelper;

/**
 * Class CartController
 */
class CartController extends AController
{
    public $modelClass = '\common\models\DdGoods';

    public function actionSearch()
    {
        return [
            'error_code'    => 20,
            'res_msg'       => 'ok',
        ];
    }

    /**
     * @SWG\Post(path="/diandi_shop/cart/add",
     *     tags={"购物车"},
     *     summary="Retrieves the collection of Goods resources.",
     *     @SWG\Response(
     *         response = 200,
     *         description = "Goods collection response",
     *     ),
     *     @SWG\Parameter(
     *     in="query",
     *     name="access-token",
     *     type="string",
     *     description="用户验证token",
     *     required=true,
     *   ),
     *     @SWG\Parameter(
     *     in="formData",
     *     name="goods_id",
     *     type="string",
     *     description="商品id",
     *     required=true,
     *   ),
     *     @SWG\Parameter(
     *     in="formData",
     *     name="num",
     *     type="integer",
     *     description="商品数量",
     *     required=true,
     *   ),
     *     @SWG\Parameter(
     *     in="formData",
     *     name="spec_id",
     *     type="string",
     *     description="规格组合id",
     *     required=false,
     *   ),
     * )
     */
    public function actionAdd()
    {
        $user_id = Yii::$app->user->identity->member_id;
        $data = Yii::$app->request->post();
        $goods_id = $data['goods_id'];
        $num = $data['num'];
        $spec_id = $data['spec_id'];
        $list = CartService::confirm($user_id, $goods_id, $num, $spec_id);

        return ResultHelper::json(200, '获取成功', $list);
    }


    /**
     * @SWG\Post(path="/diandi_shop/cart/list",
     *     tags={"购物车"},
     *     summary="获取购物车列表",
     *     @SWG\Response(
     *         response = 200,
     *         description = "获取购物车列表",
     *     ),
     *     @SWG\Parameter(
     *     in="query",
     *     name="access-token",
     *     type="string",
     *     description="用户验证token",
     *     required=true,
     *   )
     * )
     */
    public function actionList()
    {
        $user_id = Yii::$app->user->identity->member_id;
        $list = CartService::list($user_id);
        return ResultHelper::json(200, '获取成功', $list);
    }

    /**
     * @SWG\Post(path="/diandi_shop/cart/clear",
     *     tags={"购物车"},
     *     summary="清空购物车",
     *     @SWG\Response(
     *         response = 200,
     *         description = "清空购物车",
     *     ),
     *     @SWG\Parameter(
     *     in="query",
     *     name="access-token",
     *     type="string",
     *     description="用户验证token",
     *     required=true,
     *   )
     * )
     */
    public function actionClear()
    {
        $user_id = Yii::$app->user->identity->member_id;
        $list = CartService::clearAll($user_id);
        return ResultHelper::json(200, '清空成功', $list);
    }
}
